package kr.inani.android.search;

import kr.inani.android.util.BitmapMaker;
import kr.inani.android.util.MyBitmap;
import kr.inani.openapi.Book;
import kr.inani.openapi.IBook;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

public class BookItem implements Parcelable
{
	public static final String BOOK_ITEM_STRING = "BookItem";
	
	private final Book	book;
	private MyBitmap	bookImage;
	private String shelfName;
	private long bookId;
	private int order;
	private String memo;

	public BookItem(Book book, String dbShelfName, long bookId, Bitmap bitmap, int order, String memo)
	{
		this.book = book;
		this.shelfName = dbShelfName;
		this.bookId = bookId;
		this.bookImage = new MyBitmap(bitmap);
		this.order = order;
		this.memo = memo;
	}
	
	public BookItem(Book book)
	{
		this(book, "", -1, BitmapMaker.makeBitmap(book.getImage()), -1, "");
	}
	

	public BookItem(Parcel in)
	{
		book = (Book) in.readSerializable();
		bookImage = (MyBitmap) in.readSerializable();
		shelfName = in.readString();
		bookId = in.readLong();
		order = in.readInt();
		memo = in.readString();
	}
	
	public String getMemo()
	{
		return memo;
	}

	public void setMemo(String memo)
	{
		this.memo = memo;
	}
	
	public void setShelfName(String shelfName2) {
		this.shelfName = shelfName2;
	}

	public void setBookId(long bookId2)
	{
		this.bookId = bookId2;
	}
	
	public long getBookId()
	{
		return this.bookId;
	}

	public IBook getBook()
	{
		return book;
	}

	public static final Parcelable.Creator<BookItem>	CREATOR	= new Parcelable.Creator<BookItem>()
																{
																	public BookItem createFromParcel(Parcel in)
																	{
																		return new BookItem(in);
																	}

																	public BookItem[] newArray(int size)
																	{
																		return new BookItem[size];
																	}
																};

	@Override
	public int describeContents()
	{
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags)
	{
		dest.writeSerializable(book);
		dest.writeSerializable(bookImage);
		dest.writeString(shelfName);
		dest.writeLong(bookId);
		dest.writeInt(order);
		dest.writeString(memo);
	}

	public String getShelfName() {
		return shelfName;
	}
	
	public Bitmap getBitmap() {
		return bookImage.getBitmap();
	}
	
	public String getImageUrl()
	{
		return book.getImage();
	}

	public String getPubdate()
	{
		String pubDate = book.getPubdate();
		if(pubDate==null) {
			return "";
		}
		if(pubDate.length() != 8) {
			return pubDate;
		}
		
		String year = pubDate.substring(0, 4);
		String month = pubDate.substring(4, 6);
		String day = pubDate.substring(6, 8);
		return (year+"."+month+"."+day);
	}

	public String getTitle()
	{
		return book.getTitle();
	}

	public String getDescription()
	{
		return book.getDescription();
	}

	public String getLinkUrl()
	{
		return book.getLink();
	}

	public String getAuthor()
	{
		return book.getAuthor();
	}

	public String getPrice()
	{
		return book.getPrice();
	}

	public String getDiscount()
	{
		return book.getDiscount();
	}

	public String getPublisher()
	{
		return book.getPublisher();
	}

	public String getIsbn()
	{
		return book.getIsbn();
	}
	
	@Override
	public boolean equals(Object o)
	{
		BookItem from = (BookItem) o;
		return (
				this.book.getTitle().equals(from.getTitle()) &&
				this.shelfName.equals(from.shelfName) &&
				this.bookId== from.bookId
				);
		
	}

	public int getOrder() {
		return order;
	}

}
